package com.ruoyi.iot.service;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.config.RabbitMqConstant;
import com.ruoyi.iot.domain.PioltDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class IotDeviceAddProcessor {
    private final Logger logger = LoggerFactory.getLogger(IotDeviceAddProcessor.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     *
     * @param pioltDevice
     */
    public void sendMsg(PioltDevice pioltDevice){
        logger.info("IotDeviceAddProcessor send  msg    {}...",pioltDevice.getDeviceCode());
        Map<String, Object> map = new HashMap<>();
        map.put("deviceId", pioltDevice.getId());
        map.put("deviceName", pioltDevice.getDeviceName());
        map.put("deviceCode", pioltDevice.getDeviceCode());//office  cad
        map.put("typeId", pioltDevice.getDeviceType());
        Message message = MessageBuilder
                .withBody(JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8))
                .setContentEncoding(StandardCharsets.UTF_8.displayName())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .build();
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_IOT_DEVICE_ADD, "", message);
        logger.info("IotDeviceAddProcessor send msg ok...");
    }
}
